package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.CustomerGroup;

public class CustomerGroupPresenter extends AbstractPresenter<CustomerGroup> {
	final String SINGLEITEM = "customerGroup";
	final String MULTIITEM = "customerGroups";
	
	public ArrayList<CustomerGroup> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public CustomerGroup tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public CustomerGroup tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			CustomerGroup item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public CustomerGroup createItemFromJSONObject(JSONObject jsonObject){
		CustomerGroup cg = new CustomerGroup();
		try{
			cg.setAuto(jsonObject.getBoolean("customerGroupAuto"));
			cg.setDescription(jsonObject.getString("customerGroupDescription"));
			cg.setId(jsonObject.getInt("customerGroupID"));
			cg.setNotes(jsonObject.getString("customerGroupNotes"));
			cg.setObsolete(jsonObject.getBoolean("customerGroupObsolete"));
			return cg;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(CustomerGroup item){
		try{
			JSONObject json = new JSONObject();
			json.put("customerGroupAuto", item.isAuto());
			json.put("customerGroupDescription", item.getDescription());
			json.put("customerGroupID", item.getId());
			json.put("customerGroupNotes", item.getNotes());
			json.put("customerGroupObsolete", item.isObsolete());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public CustomerGroup getEmptyEntity(){
		CustomerGroup entity = new CustomerGroup();
		return entity;
	}
}
